﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.WindowsAPICodePack.Dialogs;
using Microsoft.WindowsAPICodePack.Shell;

namespace ChannelJsonConvet
{
    /// <summary>
    /// ConfigSetting.xaml 的交互逻辑
    /// </summary>
    public partial class ConfigSetting : Window
    {
        public string jsonFilePath = "";
        public string apkFilePath = "";
        public string ouputFilePath = "";
        public bool isSingleOutput = false;
        public string base_url = "";
        public string backup_base_url = "";

        public ConfigSetting()
        {
            InitializeComponent();
        }

        public ConfigSetting(ConfigData configData)
        {
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            jsonFilePath = configData.m_jsonFilePath;
            apkFilePath = configData.m_apkFilePath;
            ouputFilePath = configData.m_outputFilePath;
            isSingleOutput = configData.m_isSingleOutput;
            base_url = configData.m_base_url;
            backup_base_url = configData.m_backup_base_url;

            InitializeComponent();

            textBox_JsonDir.Text = jsonFilePath;
            textBox_ApkDir.Text = apkFilePath;
            textBox_OutputDir.Text = ouputFilePath;
            checkBox_isSingle.IsChecked = isSingleOutput;
            textBox_base_url.Text = base_url;
            textBox_backup_base_url.Text = backup_base_url;
        }

        private void button_JsonDir_Click(object sender, RoutedEventArgs e)
        {
            ShellContainer selectedFolder = null;
            selectedFolder = KnownFolders.Computer as ShellContainer;
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            dialog.InitialDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            dialog.IsFolderPicker = true;//设置为选择文件夹
            dialog.InitialDirectoryShellContainer = selectedFolder;
            dialog.EnsureReadOnly = true;

            if(dialog.ShowDialog()== CommonFileDialogResult.Ok)
            {
                jsonFilePath = dialog.FileName;
                textBox_JsonDir.Text = jsonFilePath;
            }
        }

        private void button_ApkDir_Click(object sender, RoutedEventArgs e)
        {
            ShellContainer selectedFolder = null;
            selectedFolder = KnownFolders.Computer as ShellContainer;
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            dialog.InitialDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            dialog.IsFolderPicker = true;//设置为选择文件夹
            dialog.InitialDirectoryShellContainer = selectedFolder;
            dialog.EnsureReadOnly = true;

            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                apkFilePath = dialog.FileName;
                textBox_ApkDir.Text = apkFilePath;
            }
        }

        private void button_Cancel_Click(object sender, RoutedEventArgs e)
        {
            jsonFilePath = textBox_JsonDir.Text;
            apkFilePath = textBox_ApkDir.Text;
            this.DialogResult = false;
        }

        private void button_Save_Click(object sender, RoutedEventArgs e)
        {
            jsonFilePath = textBox_JsonDir.Text;
            apkFilePath = textBox_ApkDir.Text;
            ouputFilePath = textBox_OutputDir.Text;
            isSingleOutput = checkBox_isSingle.IsChecked == true;
            base_url = textBox_base_url.Text;
            backup_base_url = textBox_backup_base_url.Text;
            this.DialogResult = true;
        }

        private void button_OutputDir_Click(object sender, RoutedEventArgs e)
        {
            ShellContainer selectedFolder = null;
            selectedFolder = KnownFolders.Computer as ShellContainer;
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            dialog.InitialDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            dialog.IsFolderPicker = true;//设置为选择文件夹
            dialog.InitialDirectoryShellContainer = selectedFolder;
            dialog.EnsureReadOnly = true;

            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                ouputFilePath = dialog.FileName;
                textBox_OutputDir.Text = ouputFilePath;
            }
        }

    }
}
